import Mock from 'mockjs'

const { newlist } = Mock.mock({
  'newlist|15':[
    {
      'userimg':'https://img2.baidu.com/it/u=1205915504,3217808836&fm=26&fmt=auto&gp=0.jpg',
      'username':'@name',
      'time':'@date(yyyy-MM-dd hh:m:ss)',
      'content':'@cparagraph',
      'dianzan|1-1000':100,
      'pinglun|1-1000':100,
      'zhuanfa|1-1000':100
    }

  ]
})
//根据url获取query参数
const getQuery = (url, name) => {
  // console.log(url,name)
  const index = url.indexOf('?')
  if (index !== -1) {//代表有参数
      const queryStrArr = url.substr(index + 1).split('&')
      for (var i = 0; i < queryStrArr.length; i++) {
          const itemArr = queryStrArr[i].split('=')
          // console.log(itemArr)
          if (itemArr[0] === name) {
              return itemArr[1]
          }
      }
  }
}
Mock.mock(/\/api\/get\/list/,'get',(Options)=>{

const pageindex = getQuery(Options.url,'pageindex')//获取pageindex

const pagesize = getQuery(Options.url,'pagesize')//获取到了pagesize
 //数据起始位置
 const start = (pageindex-1)*pagesize
 //结束位置
 const end = pageindex*pagesize
 console.log(start+"  "+end)
 const totalPage = Math.ceil(newlist.length/pagesize)
 const list =newlist.slice(start,end)

  return{
    list: list
  }
})
